import 'package:yp/config/config.dart';
import 'package:sp_util/sp_util.dart';
import 'package:yp/services/api.dart';

import 'dart:convert';
import 'dart:typed_data';
import 'package:dio/dio.dart';
import 'package:flutter/services.dart';
import 'package:yp/config/config.dart';
import 'package:yp/services/api.dart';
// import 'package:yp/lib/pages/mall_app/main_app/logic.dart';
import 'package:flutter_easyloading/flutter_easyloading.dart';
import 'package:fluwx/fluwx.dart';
import 'package:get/get.dart';
import 'package:image_gallery_saver/image_gallery_saver.dart';
import 'package:sp_util/sp_util.dart';
import 'package:fluwx/fluwx.dart' as fluwx;
import '../../../helper.dart';
import '../../../router.dart';
import 'state.dart';

class AddressInfoModel {
  int code;
  String message;
  Null full_message;
  List<ProvinceInfoModel> prv_list;
  List<CityInfoModel> city_list;
  List<CityAreaInfoModel> city_area_list;
  List<TownInfoModel> town_list;

  AddressInfoModel({
    this.code,
    this.message,
    this.full_message,
    this.prv_list,
    this.city_list,
    this.city_area_list,
    this.town_list,
  });

  AddressInfoModel.fromJson(Map<String, dynamic> json) {
    code = json['code'];
    message = json['message'];
    full_message = json['full_message'];
    if (json['prv_list'] != null) {
      prv_list = List<ProvinceInfoModel>();
      json['prv_list'].forEach((v) {
        prv_list.add(ProvinceInfoModel.fromJson(v));
      });
    }
    if (json['city_list'] != null) {
      city_list = List<CityInfoModel>();
      json['city_list'].forEach((v) {
        city_list.add(CityInfoModel.fromJson(v));
      });
    }
    if (json['city_area_list'] != null) {
      city_area_list = List<CityAreaInfoModel>();
      json['city_area_list'].forEach((v) {
        city_area_list.add(CityAreaInfoModel.fromJson(v));
      });
    }
    if (json['town_list'] != null) {
      town_list = List<TownInfoModel>();
      json['town_list'].forEach((v) {
        town_list.add(TownInfoModel.fromJson(v));
      });
    }
  }

  Map<String, dynamic> toJson() {
    final Map<String, dynamic> data = new Map<String, dynamic>();
    data['code'] = this.code;
    data['message'] = this.message;
    data['full_message'] = this.full_message;
    if (this.prv_list != null) {
      data['prv_list'] = this.prv_list.map((v) => v.toJson()).toList();
    }
    if (this.city_list != null) {
      data['city_list'] = this.city_list.map((v) => v.toJson()).toList();
    }
    if (this.city_area_list != null) {
      data['city_area_list'] =
          this.city_area_list.map((v) => v.toJson()).toList();
    }
    if (this.town_list != null) {
      data['town_list'] = this.town_list.map((v) => v.toJson()).toList();
    }
    return data;
  }
}

class ProvinceInfoModel {
  String prv_name;
  String prv_num_id;

  ProvinceInfoModel({
    this.prv_name,
    this.prv_num_id,
  });
  ProvinceInfoModel.fromJson(Map<String, dynamic> json) {
    prv_name = json['prv_name'];
    prv_num_id = json['prv_num_id'];
  }

  Map<String, dynamic> toJson() {
    final Map<String, dynamic> data = new Map<String, dynamic>();
    data['prv_name'] = this.prv_name;
    data['prv_num_id'] = this.prv_num_id;
    return data;
  }
}

class CityInfoModel {
  String city_name;
  String city_num_id;

  CityInfoModel({
    this.city_name,
    this.city_num_id,
  });

  CityInfoModel.fromJson(Map<String, dynamic> json) {
    city_name = json['city_name'];
    city_num_id = json['city_num_id'];
  }

  Map<String, dynamic> toJson() {
    final Map<String, dynamic> data = new Map<String, dynamic>();
    data['city_name'] = this.city_name;
    data['city_num_id'] = this.city_num_id;
    return data;
  }
}

class CityAreaInfoModel {
  String city_area_name;
  String city_area_num_id;

  CityAreaInfoModel({
    this.city_area_name,
    this.city_area_num_id,
  });

  CityAreaInfoModel.fromJson(Map<String, dynamic> json) {
    city_area_name = json['city_area_name'];
    city_area_num_id = json['city_area_num_id'];
  }

  Map<String, dynamic> toJson() {
    final Map<String, dynamic> data = new Map<String, dynamic>();
    data['city_area_name'] = this.city_area_name;
    data['city_area_num_id'] = this.city_area_num_id;
    return data;
  }
}

class TownInfoModel {
  String town_name;
  String town_num_id;

  TownInfoModel({
    this.town_name,
    this.town_num_id,
  });

  TownInfoModel.fromJson(Map<String, dynamic> json) {
    town_name = json['town_name'];
    town_num_id = json['town_num_id'];
  }

  Map<String, dynamic> toJson() {
    final Map<String, dynamic> data = new Map<String, dynamic>();
    data['town_name'] = this.town_name;
    data['town_num_id'] = this.town_num_id;
    return data;
  }
}

class AddressItemModel {
  String addressName;
  String addressId;

  AddressItemModel({
    this.addressName,
    this.addressId,
  });

  AddressItemModel.fromJson(Map<String, dynamic> json) {
    addressName = json['addressName'];
    addressId = json['addressId'];
  }

  Map<String, dynamic> toJson() {
    final Map<String, dynamic> data = new Map<String, dynamic>();
    data['addressName'] = this.addressName;
    data['addressId'] = this.addressId;
    return data;
  }
}

void changeSelectAddress(Map<String, dynamic> selectAddress) {
  if (selectAddress == null) {
    return;
  }

  var localLocation = SpUtil.getObject(Config.USER_CURRENT_LOCATION);
  localLocation["userSelectAddress"] = selectAddress;
  // print("设置用户选择的位置信息，用户位置是：$locationResult");
  // update();
  SpUtil.putObject(Config.USER_CURRENT_LOCATION, localLocation);
}

/// 获取用户位置信息简要描述
void getCurrentLocationInfo({success, error}) {
  String addressAllString = "";
  Map<String, dynamic> addressInfo = {};

  Map locationMap = SpUtil.getObject(Config.USER_CURRENT_LOCATION);
  var selectAddress = {};
  var requestAddress = {};

  if (locationMap != null && locationMap.length > 0) {
    selectAddress = locationMap["userSelectAddress"];
    requestAddress = locationMap["AMapResult"];
  }

  if (selectAddress != null && selectAddress.length > 0) {
    addressAllString = getAllAddressString(selectAddress);
    addressInfo = selectAddress;
    success({"addressAllString": addressAllString, "addressInfo": addressInfo});
  } else {
    requestGetAddress(success: (backInfo) {
      if (backInfo != null && backInfo.length > 0) {
        addressAllString = getAllAddressString(backInfo);
        addressInfo = backInfo;
      } else if (requestAddress != null && requestAddress.length > 0) {
        addressAllString = getAllAddressStringLocation(requestAddress);
        addressInfo = getAddressCodeInfoLocation(requestAddress);
      }
      success(
          {"addressAllString": addressAllString, "addressInfo": addressInfo});
    });
  }
}

/// 获取地址全名
String getAllAddressString(Map<String, dynamic> memberAddress) {
  String addressStr = "";
  if (memberAddress["prv_name"] != null) {
    addressStr = addressStr + memberAddress["prv_name"];
  }
  if (memberAddress["city_name"] != null) {
    addressStr = addressStr + memberAddress["city_name"];
  }
  if (memberAddress["city_area_name"] != null) {
    addressStr = addressStr + memberAddress["city_area_name"];
  }
  if (memberAddress["town_name"] != null) {
    addressStr = addressStr + memberAddress["town_name"];
  }
  if (memberAddress["adr"] != null) {
    addressStr = addressStr + memberAddress["adr"];
  }

  return addressStr;
}

/// 获取地址全名-定位
String getAllAddressStringLocation(Map<String, dynamic> locationAddress) {
  String addressStr = "";
  if (locationAddress["province"] != null) {
    addressStr = addressStr + locationAddress["province"];
  }
  if (locationAddress["city"] != null) {
    addressStr = addressStr + locationAddress["city"];
  }
  if (locationAddress["district"] != null) {
    addressStr = addressStr + locationAddress["district"];
  }
  if (locationAddress["street"] != null) {
    addressStr = addressStr + locationAddress["street"];
  }

  return addressStr;
}

/// 获取定位地址code
Map<String, dynamic> getAddressCodeInfoLocation(
    Map<String, dynamic> locationAddress) {
  Map<String, dynamic> addressCodeInfo = {};
  addressCodeInfo["prv_name"] =
      locationAddress["province"] == null ? "" : locationAddress["province"];
  addressCodeInfo["city_name"] =
      locationAddress["city"] == null ? "" : locationAddress["city"];
  addressCodeInfo["city_area_name"] =
      locationAddress["district"] == null ? "" : locationAddress["district"];
  addressCodeInfo["town_name"] =
      locationAddress["street"] == null ? "" : locationAddress["street"];

  addressCodeInfo["prv_num_id"] = "";
  addressCodeInfo["city_num_id"] = "";
  addressCodeInfo["city_area_num_id"] = "";
  addressCodeInfo["town_num_id"] = "";

  // addressCodeInfo["prv_num_id"] = locationAddress["province"];
  // addressCodeInfo["city_num_id"] = locationAddress["city"];
  // addressCodeInfo["city_area_num_id"] = locationAddress["district"];
  // addressCodeInfo["town_num_id"] = locationAddress["street"];

  addressCodeInfo["maplocation_x"] = locationAddress["longitude"];
  addressCodeInfo["maplocation_y"] = locationAddress["latitude"];

  return addressCodeInfo;
}

void requestGetAddress({success, error}) {
  Map<String, dynamic> addressInfo = {};

  var params = {
    'usr_num_id': SpUtil.getInt('empeNumId'),
    'access_token': SpUtil.getString('token'),
  };
  //获取选择商品列表数据
  Api.addressList(params).listen((event) {
    if (event['code'] == 0) {
      List list = event["member_adrs"];
      if (list != null && list.length > 0) {
        addressInfo = list[0];
        event["member_adrs"].forEach((element) {
          if (element["default_sign"] != null && element["default_sign"] == 1) {
            addressInfo = element;
          }
        });
      }
    }
    success(addressInfo);
  }, onError: (error) {
    error(error);
  });
}
